package com.network.proxy;

import com.network.proxy.adapter.CallAdapter;
import com.network.proxy.converter.Converter;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * Created by kkmike999 on 2016/06/04.
 */
public class ServiceProxy implements InvocationHandler {
	MyRetrofit retrofit;

	public ServiceProxy(MyRetrofit retrofit) {
		this.retrofit = retrofit;
	}

	@Override
	public Object invoke(Object proxy, Method method, Object... args) throws Throwable {
		// If the method is a method from Object then defer to normal invocation.
		if (method.getDeclaringClass() == Object.class) {
			return method.invoke(this, args);
		}

		ServiceMethod serviceMethod = new ServiceMethod(method);

		Converter   converter = retrofit.converter(serviceMethod.actualType);
		CallAdapter adapter   = retrofit.callAdapter();

		HttpCall call = new HttpCall(serviceMethod, converter, args);

		return adapter.adapt(call);
	}
}
